package com.example.tmxk.selvlet;

import com.example.tmxk.bean.NewsBean;
import com.example.tmxk.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/news.jsp")
public class NoticeAddSelvlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String newsTitle = req.getParameter("NewsTitle");
        String newsContent = req.getParameter("NewsContent");
        NewsBean NewsBean = new NewsBean();// 实例化新闻对象
        NewsBean.setNewsTitle(newsTitle);// 设置新闻标题
        NewsBean.setNewsContent(newsContent);//设置新闻内容
        NewsBean.setAdminName("admin");// 实例化新闻Dao对象
        NewsDao newsDao = new NewsDao();// 调用添加方法 传递新闻对象
        newsDao.add(NewsBean);
        // 在Servlet中调用使用网址跳转 请求结果对象
        req.getRequestDispatcher("news.jsp");// 参数是相对网址
    }
}
